package cetcbigdata.da.service.beijing.administrative_power_city_region;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author YuanGongPing
 * @version 0.1
 * @description UtilBeiJing
 * @since 2022/9/14 15:58
 */


public class UtilBeiJing {
    public static JSONObject getInfo(String html){
        JSONObject response = JSONObject.parseObject(html);
        if(response.getString("code").equals("200")){
            JSONObject data = response.getJSONObject("data");
            JSONObject baseInfo = new JSONObject();
            baseInfo.put("名称", data.getString("taskName"));
            baseInfo.put("职权编码", data.getString("catalogCode"));
            baseInfo.put("职权类型", getTaskType(data.getString("taskType")));
            baseInfo.put("实施主体", data.getString("deptName"));
            baseInfo.put("行使层级", getUseLevel(data.getString("useLevel")));
            baseInfo.put("发布依据", directoryLawList(data));
            baseInfo.put("政务服务事项", seviceData(data));
            return baseInfo;
        }
        return new JSONObject();
    }

    private static String getUseLevel(String type){
        String out = null;
        switch (type){
            case "1":
                out = "市级, 区级";
                break;
            case "2":
                out = "市级";
                break;
            case "3":
                out = "区级";
                break;
            default:
                out = "市级, 区级";
                break;
        }
        return out;
    }

    private static String getTaskType(String type){
        String out = null;
        switch (type){
            case "01":
                out = "行政许可";
                break;
            case "02":
                out = "行政处罚";
                break;
            case "03":
                out = "行政强制";
                break;
            case "04":
                out = "行政征收";
                break;
            case "05":
                out = "行政给付";
                break;
            case "06":
                out = "行政检查";
                break;
            case "07":
                out = "行政确认";
                break;
            case "08":
                out = "行政奖励";
                break;
            case "09":
                out = "行政裁决";
                break;
            case "10":
                out = "其他行政权力";
                break;
            case "20":
                out = "公共服务";
                break;
            case "30":
                out = "政府内部审批";
                break;
            default:
                out = "";
                break;
        }
        return out;
    }

    private static List<JSONObject> directoryLawList(JSONObject data){
        try {
            if(!data.containsKey("directoryLawList")){
                return new ArrayList<>();
            }
            JSONArray dataInfo = JSONObject.parseArray(data.getString("directoryLawList"));
            List<JSONObject> out = new ArrayList<>();
            for(Object o :dataInfo){
                JSONObject item = (JSONObject) o;
                JSONObject info = new JSONObject();
                info.put("发布号令地址", item.getString("LAW_URL"));
                info.put("发布号令", item.getString("LAW_CODE"));
                info.put("发布依据", item.getString("LAW_NAME"));
                out.add(info);
            }
            return out;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    private static List<JSONObject> seviceData(JSONObject data){
        try {
            if(!data.containsKey("taskPublishingList")){
                return new ArrayList<>();
            }
            JSONArray dataInfo = JSONObject.parseArray(data.getString("taskPublishingList"));
            List<JSONObject> out = new ArrayList<>();
            for(Object o :dataInfo){
                JSONObject item = (JSONObject) o;
                JSONObject info = new JSONObject();
                info.put("事项地址", "https://banshi.beijing.gov.cn/" + item.getString("taskUrl"));
                info.put("事项名称", item.getString("taskName"));
                info.put("实施机构", item.getString("deptName"));
                out.add(info);
            }
            return out;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    public static List<JSONObject> getRegionList(){
        String response = "{\"success\":true,\"code\":\"200\",\"data\":{\"locationName\":\"北京市\",\"orderNum\":0,\"id\":\"110000000000\",\"classification\":\"2\",\"delFlag\":\"0\",\"parentId\":\"1\",\"childNode\":[{\"locationName\":\"东城区\",\"orderNum\":1,\"id\":\"110101000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"西城区\",\"orderNum\":2,\"id\":\"110102000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"朝阳区\",\"orderNum\":3,\"id\":\"110105000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"丰台区\",\"orderNum\":4,\"id\":\"110106000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"石景山区\",\"orderNum\":5,\"id\":\"110107000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"海淀区\",\"orderNum\":6,\"id\":\"110108000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"门头沟区\",\"orderNum\":7,\"id\":\"110109000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"房山区\",\"orderNum\":8,\"id\":\"110111000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"副中心（通州区）\",\"orderNum\":9,\"id\":\"110112000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"顺义区\",\"orderNum\":10,\"id\":\"110113000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"昌平区\",\"orderNum\":11,\"id\":\"110114000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"大兴区\",\"orderNum\":12,\"id\":\"110115000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"怀柔区\",\"orderNum\":13,\"id\":\"110116000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"平谷区\",\"orderNum\":14,\"id\":\"110117000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"密云区\",\"orderNum\":15,\"id\":\"110118000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"延庆区\",\"orderNum\":16,\"id\":\"110119000000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"},{\"locationName\":\"经济开发区\",\"orderNum\":17,\"id\":\"110115403000\",\"classification\":\"3\",\"delFlag\":\"0\",\"parentId\":\"110000000000\"}]}}";
        JSONArray data = JSONObject.parseObject(response).getJSONObject("data").getJSONArray("childNode");
        List<JSONObject> out = new ArrayList<>();
        for(Object o : data){
            out.add((JSONObject) o);
        }
        return out;
    }

}
